import axios from 'axios'
import store from '../store'
import {Message} from 'element-ui'
import { getToken } from '@/utils/auth'

const http = {}

var instance = axios.create({
    timeout: 10000  // 请求超时时间 10s
})

// 添加请求拦截器
instance.interceptors.request.use(
    function (config) {
        // 请求头添加token
        if (getToken()) {
            config.headers.Authorization = 'Bearer ' + getToken()
        }
        return Promise.resolve(config)
    },
    function (error) {
        return Promise.reject(error)
    }
)
// 响应拦截器即异常处理
instance.interceptors.response.use(
    response => {
        if (response.data.code === 200){
            return Promise.resolve(response)
        } else {
            return Promise.resolve(response)
        }
    },
    err => {
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                    err.message = '请求出错'
                    break
                case 401:
                    Message.warning({
                        message: '授权失败，请重新登录'
                    })
                    store.commit('LOGIN_OUT')
                    setTimeout(() => {
                        window.location.reload()
                    }, 1000)
                    return
                case 403:
                    err.message = '拒绝访问'
                    break
                case 404:
                    err.message = '请求错误,未找到该资源'
                    break
                case 500:
                    err.message = '服务器端出错'
                    break
                default:
                    err.message = '111111'
            }
        } else {
            err.message = '连接服务器失败'
        }
        Message.error({
            message: err.message
        })
        return Promise.reject(err.response)
    }
)


http.get = function (url, options) {
    return new Promise((resolve, reject) => {
        instance
            .get(url, options)
            .then(response => {
                if (response.data.code === 200) {
                    resolve(response.data)
                } else if (response.data.code === 40006) {
                    Message.warning({
                        message: '授权失败，请重新登录'
                    })
                    store.commit('LOGIN_OUT')
                    setTimeout(() => {
                        window.location.reload()
                    }, 1000)
                } else {
                    Message.error({
                        message: response.data.message
                    })
                    reject(response.data.message)
                }
            })
            .catch(e => {
                console.log(e)
            })
    })
}

http.post = function (url, data, options) {
    return new Promise((resolve, reject) => {
        instance
            .post(url, data, options)
            .then(response => {
                if (response.data.code === 200) {
                    resolve(response.data)
                } else {
                    Message.error({
                        message: response.data.message
                    })
                    reject(response.data)
                }
            })
            .catch(e => {
                console.log(e)
            })
    })
}

export default http
